﻿package milkmidi.query
{	
	import flash.display.DisplayObject;
	import milkmidi.core.IDestroyable;	
	import flash.display.InteractiveObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;	
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import milkmidi.utils.MyTween;
	
	public class MQueryObject extends Object implements IDestroyable
	{
		internal static var dictionary:Dictionary = new Dictionary();
		
		private var _target			:InteractiveObject = null;		
		private var _hitArea		:Sprite = null;					
		private var _listenerManager:MQueryListenerManager;
		private var _enabled		:Boolean = true;
		public function MQueryObject(p_target:InteractiveObject):void {					
			dictionary[p_target] = this;
			_target = p_target;				
			_listenerManager = MQueryListenerManager.getManager(_target);			
			_target.addEventListener(Event.REMOVED_FROM_STAGE, removeEventListeners);						
		}				
		public function play2():void {
			MyTween.play2(_target);
		}
		public function playBack():void {
			MyTween.playBack(_target);
		}
		public function go(p_frame:uint):void {
			MyTween.go(_target, p_frame);
		}
		public function set onPress(p_fun:Function):void {
			_initEventHandler(p_fun, MouseEvent.MOUSE_DOWN , true);				
		}
		public function set onRelease(p_fun:Function):void {
			_initEventHandler(p_fun, MouseEvent.MOUSE_UP, true);				
		}
		public function set onRollOver(p_fun:Function):void {
			_initEventHandler(p_fun, MouseEvent.ROLL_OVER, true);				
		}
		public function set onRollOut(p_fun:Function):void {
			_initEventHandler(p_fun, MouseEvent.ROLL_OUT, true);					
		}
		public function set onClick(p_fun:Function):void {			
			_initEventHandler(p_fun, MouseEvent.CLICK, true);								
		}
		public function set onEnterFrame(p_fun:Function):void {
			_initEventHandler(p_fun, Event.ENTER_FRAME , false);
		}
		public function set onMouseMove(p_fun:Function):void {
			_initEventHandler(p_fun, MouseEvent.MOUSE_MOVE , false);
		}
		public function get enabled():Boolean {
			return _enabled;
		}
		public function set enabled(p_bol:Boolean):void {
			_listenerManager.enabled = p_bol;
			this.buttonMode = p_bol;
		}
		public function set autoOverOut(p_bol:Boolean):void {
			if (p_bol) {
				this.onRollOver = function ():void {
					play2();
				}
				this.onRollOut = function ():void {
					playBack();
				}
			}else {
				this.onRollOver = null;
				this.onRollOut = null;				
			}
		}
		public function set hitArea(p_value:InteractiveObject):void {
			if (!_isSprite()) {
				throw new Error("Object must Sprite");
			}
			
			if (p_value is Sprite) {
				Sprite(_target).hitArea = Sprite(p_value);
			}else {
				
				if (_hitArea != null) {
					_target.parent.removeChild(_hitArea);
				}
				
				_hitArea = new Sprite();
				_hitArea.name = p_value.name;
				_hitArea.x = p_value.x;
				_hitArea.y = p_value.y;
				_hitArea.addChild(p_value);
				_hitArea.mouseEnabled = false;
				_target.parent.addChild(_hitArea);
				Sprite(_target).hitArea = _hitArea;		
				
			}
		}
		public function loadMovie(p_url:String):void {
			if (_target as DisplayObjectContainer == null) {
				throw new Error("target must DisplayObjectContainer")
			}
			
			var _ldr:Loader;
			_ldr = DisplayObjectContainer(_target).getChildByName("_mqueryObject_load_mc") as Loader;
			if (_ldr != null) {
				DisplayObjectContainer(_target).removeChild(_ldr);
			}			
			_ldr = new Loader();
			_ldr.name = "_mqueryObject_load_mc";
			_ldr.load(new URLRequest(p_url));
			DisplayObjectContainer(_target).addChild(_ldr);
		}
		public function removeEventListeners(e:Event = null):void {		
			_listenerManager.destroy();
			dictionary[_target] = null;
			_target.removeEventListener(Event.REMOVED_FROM_STAGE, removeEventListeners);
			if (_hitArea != null) target.parent.removeChild(_hitArea);			
			
			delete 	dictionary[_target];
			_target = null;
		}
		public function destroy():void {
			if (_target.parent != null) {
				_target.parent.removeChild(_target);
				
				
			}
		}
		private function _initEventHandler(p_fun:Function, p_type:String ,p_buttonMode:Boolean = false):void	{					
			if (p_fun == null) {
				_listenerManager.removeEventListener(p_type, p_fun);				
				return;
			}									
			if (_listenerManager.hasEventListenerForType(p_type)) {
				_listenerManager.removeEventListener(p_type, p_fun);				
			}else {
				_listenerManager.addEventListener(p_type, p_fun);
			}
			this.buttonMode = p_buttonMode;										
		}		
		private function _isSprite():Boolean {
			if (_target is Sprite)  return true;
			else return false;
		}
		private function set buttonMode(p_bol:Boolean):void {
			if (_isSprite()) {
				Sprite(_target).buttonMode = p_bol;
			}
		}
		public function get target():DisplayObject { return _target; }	
	}
	
}
